import time
import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By
from unittest import TestCase


class TestLogin(TestCase):

    def testLogin1(self):
        connect = pymysql.connect(host='localhost', user='root', password='123456', database='finance')
        cursor = connect.cursor()
        sql = 'select username from user'
        cursor.execute(sql)
        x = cursor.fetchall()

        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)

        self.assertIn(('lisi',), x)

        # 定位用户名输入框
        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("lisi")

        # 定位密码
        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("123456")

        # 点击登陆
        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        # 获取那个标题里面的文本是不是：个人理财系统
        result = driver.find_element(By.XPATH, "//div[@id='logo']/a").text

        if result != "个人理财系统":
            driver.save_screenshot("lisi-123456.jpg")

        self.assertEqual(result, "个人理财系统")

        driver.quit()

    def testLogin2(self):
        connect = pymysql.connect(host='localhost', user='root', password='123456', database='finance')
        cursor = connect.cursor()
        sql = 'select username from admin'
        cursor.execute(sql)
        x = cursor.fetchall()
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)
        self.assertIn(('admin',), x)
        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("admin")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("123456")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        result = driver.find_element(By.XPATH, "//div[@id='logo']/a").text

        if result != "后台管理系统":
            driver.save_screenshot("admin-123456.jpg")

        self.assertEqual(result, "后台管理系统")

        driver.quit()

    def testLogin3(self):
        connect = pymysql.connect(host='localhost', user='root', password='123456', database='finance')
        cursor = connect.cursor()
        sql = 'select username from user'
        cursor.execute(sql)
        x = cursor.fetchall()
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)
        self.assertIn(('lisi',), x)
        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("lisi3")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("123456")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        result = driver.find_element(By.XPATH, "//div[1]/span[1]").text

        if result != "用户名不存在":
            driver.save_screenshot("lisi3-123456.jpg")

        self.assertEqual(result, "用户名不存在")

        driver.quit()

    def testLogin4(self):
        connect = pymysql.connect(host='localhost', user='root', password='123456', database='finance')
        cursor = connect.cursor()
        sql = 'select username from user'
        cursor.execute(sql)
        x = cursor.fetchall()
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)
        self.assertIn(('lisi',), x)
        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("lisi")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("1234567")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        result = driver.find_element(By.XPATH, "//div[2]/span[1]").text

        if result != "密码错误":
            driver.save_screenshot("lisi-1234567.jpg")

        self.assertEqual(result, "密码错误")

        driver.quit()

    def testLogin5(self):
        connect = pymysql.connect(host='localhost', user='root', password='123456', database='finance')
        cursor = connect.cursor()
        sql = 'select username from admin'
        cursor.execute(sql)
        x = cursor.fetchall()
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)
        self.assertIn(('admin',), x)
        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("admin1")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("1234567")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        result = driver.find_element(By.XPATH, "//div[1]/span[1]").text

        if result != "用户名不存在":
            driver.save_screenshot("admin1-123456.jpg")

        self.assertEqual(result, "用户名不存在")

        driver.quit()

    def testLogin6(self):
        connect = pymysql.connect(host='localhost', user='root', password='123456', database='finance')
        cursor = connect.cursor()
        sql = 'select username from admin'
        cursor.execute(sql)
        x = cursor.fetchall()
        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.get("http://localhost:90")

        time.sleep(2)
        self.assertIn(('admin',), x)
        ele1 = driver.find_element(By.XPATH, "//input[@id='username']")
        ele1.send_keys("admin")

        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys("1234567")

        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()

        time.sleep(3)

        result = driver.find_element(By.XPATH, "//div[2]/span[1]").text

        if result != "密码错误":
            driver.save_screenshot("admin-1234567.jpg")

        self.assertEqual(result, "密码错误")

        driver.quit()
